// mela onlie 购物车界面 订制
import { PureComponent } from "react";
import { Button, Modal } from "antd-mobile"
import { connect } from 'dva';
import { InputItem } from 'antd-mobile';
import { handleImg, isNull, backToPrevious,goToPage,handleOSS } from "../../utils/helper";
import MToast from '../../components/Util/MToast';
import "./PlaceOrder.less"
@connect(() => {
  return ({

  })
})
export default class AddAddress extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      recivier: "",//(收货人)
      street: "",//(街道)
      city: "",//(城市)
      district: "",//(行政区)
      country: "IT",//(国家)
      post_code: "",//(邮编)
      concat: "",//(联系电话)
      objItems:[]
    }
  }
  componentWillMount() {
    let { dispatch, location } = this.props;
    dispatch({
      type: "global/setHeaderTitle",
      payload: {
        title: T("Aggiungi nuovo indirizzo(新增地址)"),
        path: location.pathname + location.search
      }
    });
    // let old_cart_address = localStorage.getItem(MI_APP_CODE + "old_cart_address");
    // if (!isNull(old_cart_address)) {
    //   localStorage.removeItem(MI_APP_CODE + "old_cart_address");
    //   old_cart_address = JSON.parse(old_cart_address);
    //   for (let i in old_cart_address) {
    //     this.setState({
    //       [i]: old_cart_address[i]
    //     })
    //   }
    // }
    localStorage.removeItem(MI_APP_CODE + "cart_address")
  }

  componentDidMount() {

  }

  submitAddress = () => {
    let { recivier, street, city, district, country, post_code, concat,objItems } = this.state;
    let { dispatch } = this.props;
    if (isNull(recivier)) {
      MToast.fail(T("Per favore inserisci un nome.（请输入姓名）"), 3, null, false);
      return;
    }
    if (isNull(street)) {
      MToast.fail(T("Per favore inserisci la via（（请填写街道）"), 3, null, false);
      return;
    }
    if (isNull(city)) {
      MToast.fail(T("Per favore inserisci la città.（请填写城市）"), 3, null, false);
      return;
    }
    if (isNull(district)) {
      MToast.fail(T("Per favore inserisci la regione.（请输入区域）"), 3, null, false);
      return;
    }
    if (isNull(post_code)) {
      MToast.fail(T("Per favore inserisci la CAP,（请输入邮编）"), 3, null, false);
      return;
    }
    if (isNull(concat)) {
      MToast.fail(T("Per favore inserisci la Numero di telefono.（请输入联系电话）"), 3, null, false);
      return;
    }
    dispatch({
      type: "form/query",
      payload: {
        apiInfo: {
          url: "/ent/create?_c=app_user_address",
          params: [
            {
              key: "recivier",
              value: recivier
            },
            {
              key: "street",
              value: street
            },
            {
              key: "city",
              value: city
            },
            {
              key: "district",
              value: district
            },
            {
              key: "country",
              value: "IT"
            },
            {
              key: "post_code",
              value: post_code
            },
            {
              key: "concat",
              value: concat
            }
          ],
          _c: "app_user_address"
        },
        callback: (result) => {
          let { data } = result;
          localStorage.setItem("saveId",data.save_id);//判断地址列表颜色的变化
          // var objArr = [recivier,street,city,district,'IT',post_code,concat];
          var obj={
            id:localStorage.getItem("saveId"),
            recivier:recivier,
            street:street,
            city:city,
            district:district,
            country:'IT',
            post_code:post_code,
            concat:concat
          }

          // localStorage.setItem(MI_APP_CODE + "cart_address",JSON.stringify(obj));
          // console.log(localStorage.getItem(MI_APP_CODE + "cart_address"));
          // if(browser.isInApp){
          //   Func.registerHanderBridge("callbackEventBack",this.changeAddress)
          // }
          // goToPage({ dispatch, config: { url: "/addressList" }, isReplace: false })
          localStorage.setItem(MI_APP_CODE + "cart_address",JSON.stringify(obj));
          backToPrevious();
        }
      }
    })

    // backToPrevious();
    // goToPage({ dispatch, config: { url: "/placeOrder" }, isReplace: false })
  }
  render() {
    let { recivier, street, city, district, country, post_code, concat } = this.state;
    return (
      <div className="normal" style={{ background: "#fff", height: "100%" }}>
        <div style={{padding:'17px 16px'}}>
          <div style={{color:'#2E2D35',fontSize:'17px',fontWeight:'bold',marginBottom:'25px'}}>新增收货地址</div>
          <div style={{border:'1px solid #EEEDF2',borderRadius:'5px',position:'relative',marginBottom:'25px'}}>
            <div style={{color:'#9C9BAB',fontSize:'11px',position:'absolute',top:'-19px',left:'23px',zIndex:'10',background:'#fff',padding:'8px'}}>Via e numero civico 街道</div>
            <InputItem
              clear
              value={street}
              onChange={(e) => {
                this.setState({
                  street: e
                })
              }}
              className="required"
              placeholder={T("Via e numero civico（填写街道）")}
            ></InputItem>
          </div>
          <div style={{}}>
            <div style={{border:'1px solid #EEEDF2',borderRadius:'5px',position:'relative',marginBottom:'25px',marginRight:'5%',width:'45%',display:'inline-block'}}>
              <div style={{color:'#9C9BAB',fontSize:'11px',position:'absolute',top:'-19px',left:'23px',zIndex:'10',background:'#fff',padding:'8px'}}>CAP 邮编</div>
              <InputItem
                clear
                value={post_code}
                onChange={(e) => {
                  this.setState({
                    post_code: e
                  })
                }}
                className="required"
                placeholder={T("CAP(邮编)")}
              ></InputItem>
            </div>
            <div style={{border:'1px solid #EEEDF2',borderRadius:'5px',position:'relative',marginBottom:'25px',width:'49%',display:'inline-block'}}>
              <div style={{color:'#9C9BAB',fontSize:'11px',position:'absolute',top:'-19px',left:'23px',zIndex:'10',background:'#fff',padding:'8px'}}>regione 区域</div>
              <InputItem
                clear
                value={district}
                onChange={(e) => {
                  this.setState({
                    district: e
                  })
                }}
                className="required"
                placeholder={T("regione(区域)")}
              ></InputItem>
            </div>
          </div>
          <div style={{border:'1px solid #EEEDF2',borderRadius:'5px',position:'relative',marginBottom:'25px'}}>
            <div style={{color:'#9C9BAB',fontSize:'11px',position:'absolute',top:'-19px',left:'23px',zIndex:'10',background:'#fff',padding:'8px'}}>Città 填写城市</div>
            <InputItem
              clear
              value={city}
              onChange={(e) => {
                this.setState({
                  city: e
                })
              }}
              className="required"
              placeholder={T("Città(填写城市)")}
            ></InputItem>
          </div>
          <div style={{border:'1px solid #EEEDF2',borderRadius:'5px',position:'relative',marginBottom:'25px'}}>
            <div style={{color:'#9C9BAB',fontSize:'11px',position:'absolute',top:'-19px',left:'23px',zIndex:'10',background:'#fff',padding:'8px'}}>Nome e cognome 姓名</div>
            <InputItem
              clear
              value={recivier}
              onChange={(e) => {
                this.setState({
                  recivier: e
                })
              }}
              className="required"
              placeholder={T("Nome e cognome 姓名")}
            ></InputItem>
          </div>
          <div style={{border:'1px solid #EEEDF2',borderRadius:'5px',position:'relative',marginBottom:'25px'}}>
            <div style={{color:'#9C9BAB',fontSize:'11px',position:'absolute',top:'-19px',left:'23px',zIndex:'10',background:'#fff',padding:'8px'}}>Enter country code 请输入</div>
            <InputItem
              clear
              disabled
              value="IT"
              className="required"
              placeholder={T("Enter country code(请输入)")}
            ></InputItem>
          </div>

          <div style={{border:'1px solid #EEEDF2',borderRadius:'5px',position:'relative',marginBottom:'25px'}}>
            <div style={{color:'#9C9BAB',fontSize:'11px',position:'absolute',top:'-19px',left:'23px',zIndex:'10',background:'#fff',padding:'8px'}}>Numero di telefono 联系电话</div>
            <InputItem
              clear
              value={concat}
              onChange={(e) => {
                this.setState({
                  concat: e
                })
              }}
              className="required"
              placeholder={T("Numero di telefono(联系电话)")}
            ></InputItem>
          </div>


          <Button size="default" type="primary" style={{ width: "80%", fontSize: "18px",margin:'0 auto',background:'#F09600',border:'0',borderRadius:'15px' }}
            onClick={() => {
              this.submitAddress()
            }}
          >{T("Salva e usa(保存并使用)")}</Button>

        </div>
      </div >
    )
  }
}
